package org.example.springnetty.MyNetty.sharedHandler;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

/**
 * 统计服务器接收和发出的报文总数
 */
@ChannelHandler.Sharable
public class MessageCountHandler extends ChannelDuplexHandler {
    private static final Logger log = Logger.getLogger(MessageCountHandler.class.getName());
    private AtomicInteger incount = new AtomicInteger(0);
    private AtomicInteger outcount = new AtomicInteger(0);

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        log.info("收到报文总数: "+incount.incrementAndGet());
        super.channelRead(ctx, msg);
    }

    @Override
    public void flush(ChannelHandlerContext ctx) throws Exception {
        log.info("发出报文总数: "+outcount.incrementAndGet());
        super.flush(ctx);
    }
}
